import {
	clearCache
} from "./clearCache";
export const indexedDb = (dbName, data, StoreName, keyPath) => {
	let db;
	let request;
	const version = 3;
	request = indexedDB.open(dbName, version);
	request.onerror = (event) =>
		console.log(`Failed to open: ${event.target.errorCode}`);
	request.onsuccess = (event) => {
		db = event.target.result;
		if (db.objectStoreNames.contains(StoreName)) {
			SavaData(StoreName, data);
			upData(StoreName, data);
		} else {}
	};

	request.onupgradeneeded = (event) => {
		const db = event.target.result;

		// 如果存在则删除当前 objectStore。测试的时候可以这样做
		// 但这样会在每次执行事件处理程序时删除已有数据
		if (db.objectStoreNames.contains(StoreName)) {
			db.deleteObjectStore(StoreName);
			upData(StoreName, data);
		} else {
			db.createObjectStore(StoreName, {
				keyPath: keyPath,
			});
		}
	};
	let SavaData = (StoreName, data) => {
		const transaction = db.transaction(StoreName, "readwrite"),
			store = transaction.objectStore(StoreName);
		if (data.length > 1) {
			for (let item of data) {
				let requests;
				requests = store.add(item);
			}
		} else if (data.length > 0) {
			let requests;

			if (Object.prototype.toString.call(data) == "[object Array]") {
				requests = store.add(...data);
			} else {
				requests = store.add(data);
			}
		} else {
			return;
		}
	};
	let upData = (StoreName, data) => {
		const transaction = db.transaction(StoreName, "readwrite"),
			store = transaction.objectStore(StoreName);
		if (data.length > 1) {
			for (let item of data) {
				let requests;
				requests = store.put(item);
			}
		} else if (data.length > 0) {
			let requests;
			if (Object.prototype.toString.call(data) == "[object Array]") {
				requests = store.put(...data);
			} else {
				requests = store.put(data);
			}
		} else {
			return;
		}
	};
};

export const getData = (dbName, StoreName, Pagination) => {
	let db;
	let request;
	let dataBase = [];
	const version = 3;
	request = indexedDB.open(dbName, version);
	request.onerror = (event) => console.log("err");
	request.onsuccess = (event) => {
		db = event.target.result;

		const transaction = db.transaction(StoreName, "readwrite"),
			store = transaction.objectStore(StoreName);
		store.getAll().onsuccess = function(event) {
			let cursor = event.target.result;
			if (cursor) {
				for (
					let i = (Pagination.curPage - 1) * Pagination.pageSize; i < Pagination.pageSize *
					Pagination.curPage && i < cursor.length; i++
				) {
					if (i > Pagination.dataTotal - 1) {
						break;
					}
					dataBase.push(cursor[i]);
				}
			} else {
				console.log("没有更多数据了！");
			}
		};
	};
	return dataBase;
};
export const getUserData = (dbName, StoreName, id) => {
	let db;
	let request;
	const version = 3;
	request = indexedDB.open(dbName, version);

	return new Promise((resolve, reject) => {
		request.onerror = (event) => console.log(event);
		request.onsuccess = (event) => {
			db = event.target.result;
			
			try {
				const transaction = db.transaction(StoreName, "readwrite"),
					store = transaction.objectStore(StoreName);
				store.get(id).onsuccess = (event) => {
					
					resolve(event.target.result);
				};
				store.get(id).onerror = (event) => {
					reject(event);
				};
			} catch (error) {
				clearCache();
			}
		};
	});
};

export const getExamine = (dbName, StoreName, Pagination) => {
	let db;
	let request;
	const version = 3;
	request = indexedDB.open(dbName, version);

	return new Promise((resolve, reject) => {
		request.onerror = (event) => console.log("err");
		request.onsuccess = (event) => {
			db = event.target.result;

			const transaction = db.transaction(StoreName, "readwrite"),
				store = transaction.objectStore(StoreName);

			store.getAll().onsuccess = (event) => {
				let cursor = event.target.result;
				let dataBase = [];
				if (cursor) {
					for (
						let i = (Pagination.curPage - 1) * Pagination.pageSize; i < Pagination
						.pageSize * Pagination.curPage && i < cursor.length; i++
					) {
						if (i > Pagination.dataTotal - 1) {
							break;
						}
						dataBase.push(cursor[i]);
					}

					resolve(dataBase);
				} else {
					console.log("没有更多数据了！");
				}
			};
			store.getAll().onerror = (event) => {
				reject(event);
			};
		};
	});
};

export const DeleteDataBase = (dbName, StoreName) => {
	let db;
	let request;
	const version = 3;
	request = indexedDB.open(dbName, version);
	request.onerror = (event) =>
		console.log(`Failed to open: ${event.target.errorCode}`);
	request.onsuccess = (event) => {
		db = event.target.result;
		// StoreIsExist(db,StoreName,keyPath)
		if (db.objectStoreNames.contains(StoreName)) {
			const transaction = db.transaction(StoreName, "readwrite");
			let store = transaction.objectStore(StoreName);
			store.clear();
		}
	};
};

// let StoreIsExist=(db,StoreName,keyPath)=>{
//     if (db.objectStoreNames.contains(StoreName)) {
//         db.deleteObjectStore(StoreName);

//     }else{
//         db.createObjectStore(StoreName, { keyPath: keyPath });

//     }
// }
